package gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import model.DelBehandlingPakkeKlar;
import model.DelBehandlingTorreTid;
import model.MellemVare;
import model.TimeStamp;
import service.Service;
/**
 * 
 * @author Martin Sax og Daniel Nissen
 *
 */
public class FindVare extends JPanel implements CarlettiPanel {
	private JPanel panel;
	private JPanel panel_1;
	private JScrollPane scrollPane;
	private JList list;
	private JScrollPane scrollPane_1;
	private JList list_1;
	private JLabel lblVareType;
	private JLabel lblVareNummer;
	private JLabel lblDelbehandlingsNummer;
	private JLabel lblMinimumTrretid;
	private JLabel lblIdeelTrretid;
	private JLabel lblMaximumTrretid;
	private JLabel lblTypetxt;
	private JLabel lblVarenrtxt;
	private JLabel lblDelnrtxt;
	private JLabel lblMintxt;
	private JLabel lblIdeeltxt;
	private JLabel lblMaxtxt;

	/**
	 * Create the panel.
	 */
	public FindVare() {
		setLayout(new BorderLayout(0, 0));

		panel = new JPanel();
		add(panel, BorderLayout.CENTER);
		panel.setLayout(new GridLayout(0, 2, 0, 0));

		scrollPane = new JScrollPane();
		panel.add(scrollPane);

		list = new JList();
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.addListSelectionListener(new ListListSelectionListener());
		scrollPane.setViewportView(list);

		scrollPane_1 = new JScrollPane();
		panel.add(scrollPane_1);

		list_1 = new JList();
		list_1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list_1.addListSelectionListener(new List_1ListSelectionListener());
		scrollPane_1.setViewportView(list_1);

		panel_1 = new JPanel();
		add(panel_1, BorderLayout.EAST);
		panel_1.setLayout(new GridLayout(0, 2, 0, 0));

		lblVareType = new JLabel("Vare type:");
		panel_1.add(lblVareType);

		lblTypetxt = new JLabel("typetxt");
		panel_1.add(lblTypetxt);

		lblVareNummer = new JLabel("Vare nummer:");
		panel_1.add(lblVareNummer);

		lblVarenrtxt = new JLabel("varenrtxt");
		panel_1.add(lblVarenrtxt);

		lblDelbehandlingsNummer = new JLabel("Delbehandlings nummer:");
		panel_1.add(lblDelbehandlingsNummer);

		lblDelnrtxt = new JLabel("delnrtxt");
		panel_1.add(lblDelnrtxt);

		lblMinimumTrretid = new JLabel("Minimum tørretid:");
		panel_1.add(lblMinimumTrretid);

		lblMintxt = new JLabel("mintxt");
		panel_1.add(lblMintxt);

		lblIdeelTrretid = new JLabel("Ideel tørretid:");
		panel_1.add(lblIdeelTrretid);

		lblIdeeltxt = new JLabel("ideeltxt");
		panel_1.add(lblIdeeltxt);

		lblMaximumTrretid = new JLabel("Maximum tørretid:");
		panel_1.add(lblMaximumTrretid);

		lblMaxtxt = new JLabel("maxtxt");
		panel_1.add(lblMaxtxt);

		update();
	}

	public void update() {
		list.setListData(Service.getAllMellemVare().toArray());
	}

	private void updadeDelBehandling(MellemVare m) {
		List<Object> listStamp = new ArrayList<Object>();
		int i = 0;
		while (i < m.getAllTimeStamp().length) {
			if (m.getAllTimeStamp()[i] != null) {
				listStamp.add(m.getAllTimeStamp()[i]);
			} else if(i ==  m.getAllTimeStamp().length - 1){
				listStamp.add("Pakkes snart");
			}else{
				listStamp.add(m.getBehandling().getDelBehandlinger().get(i));
			}
			i++;
		}
		list_1.setListData(listStamp.toArray());
		list_1.setSelectedIndex(0);
	}

	private class ListListSelectionListener implements ListSelectionListener {
		public void valueChanged(ListSelectionEvent arg0) {
			if (list.getSelectedIndex() >= 0) {
				updadeDelBehandling(Service.getAllMellemVare().get(
						list.getSelectedIndex()));
				lblTypetxt.setText(Service.getAllMellemVare().get(
						list.getSelectedIndex()).getNavn());
				lblVarenrtxt.setText(Service.getAllMellemVare().get(
						list.getSelectedIndex()).getVareNummer()
						+ "");
				lblDelnrtxt
						.setText(Service.getAllMellemVare().get(
								list.getSelectedIndex()).getNextDelBehandling()
								+ " ud af "
								+ Service.getAllMellemVare().get(
										list.getSelectedIndex())
										.getAllTimeStamp().length);
			}
		}

	}

	private class List_1ListSelectionListener implements ListSelectionListener {
		public void valueChanged(ListSelectionEvent arg0) {
			if (list_1.getSelectedIndex() >= 0) {
				if (((MellemVare) list.getSelectedValue()).getAllTimeStamp()[list_1
						.getSelectedIndex()] != null) {
					if (((TimeStamp) list_1.getSelectedValue())
							.getDelBehandling() instanceof DelBehandlingTorreTid) {
						DelBehandlingTorreTid del = (DelBehandlingTorreTid) ((TimeStamp) list_1
								.getSelectedValue()).getDelBehandling();
						lblMintxt.setText(del.getMinTid() + "");
						lblIdeeltxt.setText(del.getIdeelTid() + "");
						lblMaxtxt.setText(del.getMaxTid() + "");
					} else if (((TimeStamp) list_1.getSelectedValue())
							.getDelBehandling() instanceof DelBehandlingPakkeKlar) {
						lblMintxt.setText("");
						lblIdeeltxt.setText("");
						lblMaxtxt
								.setText(((DelBehandlingPakkeKlar) ((TimeStamp) list_1
										.getSelectedValue()).getDelBehandling())
										.getMaxTid()
										+ "");
					} else {
						lblMintxt.setText("");
						lblIdeeltxt.setText("");
						lblMaxtxt.setText("");
					}

				} else {
					if (list_1.getSelectedValue() instanceof DelBehandlingTorreTid) {
						DelBehandlingTorreTid del = (DelBehandlingTorreTid) list_1
								.getSelectedValue();
						lblMintxt.setText(del.getMinTid() + "");
						lblIdeeltxt.setText(del.getIdeelTid() + "");
						lblMaxtxt.setText(del.getMaxTid() + "");
					} else if (list_1.getSelectedValue() instanceof DelBehandlingPakkeKlar) {
						lblMintxt.setText("");
						lblIdeeltxt.setText("");
						lblMaxtxt.setText(""
								+ ((DelBehandlingPakkeKlar) list_1
										.getSelectedValue()).getMaxTid());
					} else {
						lblMintxt.setText("");
						lblIdeeltxt.setText("");
						lblMaxtxt.setText("");
					}
				}

			}
		}
	}
}
